package com.isoft.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ImportFailException.class)
    public ResponseEntity<Object> handleImportFail(ImportFailException ex) {
        Map<String, Object> data = new HashMap<>();
        data.put("message", ex.getMessage());
        data.put("details", ex.getErrorDetails());

        // 返回与成功相同的结构：{ code:1, data:{ message, details } }，但状态码为422
        com.isoft.result.Result<Object> wrapper = com.isoft.result.Result.success(data);
        return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(wrapper);
    }
}


